from nornir import InitNornir
from nornir.core.plugins.inventory import InventoryPluginRegister
from cmdb_inventory_plugin_v1 import CMDBInventory

InventoryPluginRegister.register("cmdb_inventory", CMDBInventory)


def get_nornir_obj(devices_obj):
    devices_obj_list = []
    for dev in devices_obj:
        dev_info = {
            'device_id': dev.get('pk'),
            'ip': dev.get('ip_address'),
            'name': dev.get('device_name'),
            'port': dev.get('connection_type'),
            'username': dev.get('username'),
            'password': dev.get('password'),
            'device_type': dev.get('platform'),
            'secret': dev.get('enable_password'),
            'type': dev.get('device_type'),
            'model': dev.get('model'),
            'vendor': dev.get('vendor'),
            'global_delay_factor': dev.get('global_delay_factor')
        }
        devices_obj_list.append(dev_info)

    runner = {
        'plugin': 'threaded',
        'options': {
            'num_workers': 100,
        }
    }

    inventory = {
        'plugin': 'cmdb_inventory',
        'options': {
            'devices': devices_obj_list
        }
    }
    nr = InitNornir(runner=runner, inventory=inventory, )
    return nr
